﻿Imports DAO
Imports GUI
Imports DTO
Imports DAO.DataProvider

Public Class ucThayDoiThongTinThuoc
    Dim DT_ThongTinThuoc As New DataTable()
    Private Sub ucThayDoiThongTinThuoc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim query_ As String
        Dim DTP As New DataProvider()
        query_ = " select * from THUOC "
        DT_ThongTinThuoc = DTP.Datatable_PHONGMACHTU(query_)
        dtvthongtinthuoc.DataSource = DT_ThongTinThuoc

    End Sub

    Private Sub dtvthongtinthuoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtvthongtinthuoc.Click
        If (dtvthongtinthuoc.CurrentRow.Cells("MaThuoc").Value.ToString <> "") Then
            txtnhapdongia.Text = dtvthongtinthuoc.CurrentRow.Cells("DonGia").Value
            txtnhaptenthuoc.Text = dtvthongtinthuoc.CurrentRow.Cells("TenThuoc").Value
            txtmathuoc.Text = dtvthongtinthuoc.CurrentRow.Cells("MaThuoc").Value
        Else
            Return
        End If
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim ThuocDTO As New ThuocDTO()
        Dim ThuocBUS As New ThuocBUS()
        Try
            ThuocDTO.TenThuoc = txtnhaptenthuoc.Text
            ThuocDTO.DONGIA = Long.Parse(txtnhapdongia.Text)
        Catch ex As Exception
            MessageBox.Show("Loi du lieu")
            Return
        End Try
        Dim kq As Integer
        kq = ThuocBUS.INSERT(ThuocDTO)
        If (kq = 1) Then
            MessageBox.Show("insert thành công")
            ucThayDoiThongTinThuoc_Load(sender, e)
        Else
            MessageBox.Show("insert ko thanh cong")
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim thuoc As New ThuocDTO()
        Try
            thuoc.MaThuoc = Long.Parse(txtmathuoc.Text)
        Catch ex As Exception
            MessageBox.Show("Khong Lay Duoc MA Thuoc De~ Xoa")
            Return
        End Try
        Dim ThuocBUS As New ThuocBUS()
        Dim kq As Integer
        kq = ThuocBUS.DELETE(thuoc)
        If (kq = 1) Then
            MessageBox.Show("Delete thanh cong")
            ucThayDoiThongTinThuoc_Load(sender, e)
        Else
            MessageBox.Show("Delete Khong Thanh Cong")
        End If
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim thuoc As New ThuocDTO()
        Try
            thuoc.MaThuoc = Long.Parse(txtmathuoc.Text)
            thuoc.TenThuoc = txtnhaptenthuoc.Text
            thuoc.DONGIA = Long.Parse(txtnhapdongia.Text)
        Catch ex As Exception
            MessageBox.Show("Loi Lay Du~ lieu")

            Return
        End Try
        Dim ThuocBUS As New ThuocBUS()
        Dim kq As Integer
        kq = ThuocBUS.UPDATE(thuoc)
        If (kq = 1) Then
            MessageBox.Show("Update thanh cong")
            ucThayDoiThongTinThuoc_Load(sender, e)
        Else
            MessageBox.Show("Update Khong Thanh Cong")
        End If
    End Sub
End Class
